#ifndef USEEEER_LIST_MANGER_H
#define USEEEER_LIST_MANGER_H

#include "SingleClass.h"
#include <memory>
#include "tcp_connection.h"
#include <shared_mutex>
#include <unordered_map>

class user_list_manger:public SingleClass<user_list_manger>
{

public:
    void add_tcp_connection(const std::string & email,const std::shared_ptr<tcp_connection>  &con);
    void remove_tcp_connection(const std::string & email);
    std::shared_ptr<tcp_connection> find_and_get_con(const std::string &email);
    //这两个的初始化会在main函数中从配置文件中拿
    std::string char_server_field;
    std::string user_list_key;
    std::string rpc_service_address;

    void update_client_server_cache(const std::string &email,const std::string &ip_port);
    std::string get_client_server_cache(const std::string &email);
    void delete_one_client_server_cache(const std::string &email);
    

private:
    user_list_manger()=default;
    friend class SingleClass<user_list_manger>;
    std::unordered_map<std::string,std::shared_ptr<tcp_connection>> local_user_list_map;  //用于存储当前服务的客户端
    //std::mutex mtx;
    std::shared_mutex mtx;
    
    //缓存用户在其他chat_server所在的信息  key：用户email   value: 用户所在的chat_server的ip_port
    std::unordered_map<std::string,std::string> client_server_cache;
    std::shared_mutex mtx_client_server_cache;

};





#endif